<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 新增弹框 -->
      <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="closeDialog">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="closeDialog">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="showDialog = true"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="name"
            label="名称"
            width="240"
          />
          <el-table-column align="center" prop="description" label="描述" />
          <el-table-column align="center" label="操作" width="240">
            <template #default="{row}">
              <el-button size="small" type="success" @click="clickShowAssignDialog(row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <!-- 放置分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height:60px">
          <el-pagination
            :current-page="page.page"
            :page-size="page.pagesize"
            :total="page.total"
            layout="prev, pager, next"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
    <CloseAssign ref="AssignForm" :role-id="roleId" :show-assign-dialog="showAssignDialog" @close-dialog-assign="closeDialogAssign" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
import CloseAssign from './components/close-assign'
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    CloseAssign
  },
  data() {
    return {
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      },
      showAssignDialog: false,
      roleId: ''
    }
  },
  mounted() {
    this.getRoleList() // 获取角色列表
  },
  methods: {
    // 关闭分配弹框
    closeDialogAssign() {
      this.showAssignDialog = false
    },
    // 分配权限
    async clickShowAssignDialog(id) {
      this.showAssignDialog = true
      this.roleId = id
      const res = await getPermissionList()
      this.$refs.AssignForm.permissionData = tranListToTreeData(res)
    },
    // 编辑按钮
    async edit(id) {
      const res = await getRoleDetail(id)
      this.showDialog = true
      this.roleForm = res
    },
    // 弹窗 关闭的回调
    closeDialog() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.roleForm = {}
    },
    // 获取角色
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 页码发生改变触发
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    deleteRole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除接口
        await deleteRole(id)
        // 更新列表
        this.getRoleList()
        // 提示用户
        this.$message.success('删除角色成功')
      }).catch()
    },
    // 添加新角色
    btnOK() {
      this.$refs.roleForm.validate(async isOk => {
        if (isOk) {
          if (this.roleForm.id) {
            await updateRole(this.roleForm)
            this.$message.success('更新角色成功')
          } else {
            await addRole(this.roleForm)
            this.$message.success('新增角色成功')
          }
          this.closeDialog()
          this.getRoleList()
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
